package com.example.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.common.utils.R;
import com.example.questionnaire.constant.ExpenseConstants;
import com.example.questionnaire.constant.TeamConstants;
import com.example.questionnaire.dao.AnswererTeamDao;
import com.example.questionnaire.dao.UserTeamDao;
import com.example.questionnaire.entity.AnswererTeamEntity;
import com.example.questionnaire.entity.UserTeamEntity;
import com.example.questionnaire.service.AnswererTeamService;
import com.example.questionnaire.service.UserService;
import com.example.questionnaire.service.UserTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.questionnaire.dao.TeamDao;
import com.example.questionnaire.entity.TeamEntity;
import com.example.questionnaire.service.TeamService;

import static com.example.questionnaire.constant.TeamConstants.*;


@Service("teamService")
public class TeamServiceImpl extends ServiceImpl<TeamDao, TeamEntity> implements TeamService {
    @Autowired
    TeamService teamService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TeamEntity> wrapper = new QueryWrapper<TeamEntity>();
        wrapper.eq("is_deleted",0);
        if(params.get("personnelId")!= null){
            wrapper.eq("personnel_id",params.get("personnelId"));
        }
        if (params.get("kind") != null) {
            wrapper.like("kind", params.get("kind"));
        }
        if(params.get("sign") != null){
            wrapper.eq("sign",params.get("sign"));
        }

        IPage<TeamEntity> page = this.page(
                new Query<TeamEntity>().getPage(params),
                wrapper
        );


        return new PageUtils(page);
    }
    @Autowired
    AnswererTeamService answererTeamService;
    @Autowired
    UserTeamService userTeamService;
    @Autowired
    UserService userService;
    @Override
    public int addTeam(TeamEntity team) {
        if(team == null){
            return 404;
        }
        if(team.getSign() == answerer.getStatenum()){
            this.save(team);
        }else{
            this.save(team);
        }
        userService.expenseUtoT(team.getPersonnelId(), ExpenseConstants.createTeamExpense.getStatenum());
        return 0;
    }
    @Autowired
    AnswererTeamDao answererTeamDao;
    @Override
    //查询答者群组里是否还有答者（返回答者数量）
    public int removeTeamInAnswerTeam(int teamId) {
        QueryWrapper<AnswererTeamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id",teamId);
        return answererTeamDao.selectCount(wrapper);
    }
    @Autowired
    UserTeamDao userTeamDao;
    @Override
    //查询用户群组里是否还有用户（返回用户数量）
    public int removeTeamInUserTeam(int teamId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("team_id",teamId);
        return userTeamDao.selectCount(wrapper);
    }

    //删除群组
    public int removeTeam(TeamEntity team) {
        if(team == null){
            return 404;
        }
        int memberNum;
        if(team.getSign() == answerer.getStatenum()){
            memberNum = removeTeamInAnswerTeam(team.getId());
        }else {
            memberNum = removeTeamInUserTeam(team.getId());
        }
        if(memberNum != 0){
            return 1;//群组里还有成员
        }else {
            UpdateWrapper<TeamEntity> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",team.getId());
            wrapper.set("is_deleted",1);
            teamService.update(wrapper);
            return 0;
        }
    }







}